package com.zkyc.framework.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.mapper.MediaSubjectMapper;
import com.zkyc.framework.common.pojo.MediaSubject;
import com.zkyc.framework.common.utils.WebsiteUtil;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.website.service.MediaSubjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (MediaSubject)表服务实现类
 *
 * @author libingchuan
 * @since 2022-06-07 14:22:53
 */
@Service("mediaSubjectService")
public class MediaSubjectServiceImpl implements MediaSubjectService {
    @Resource
    private MediaSubjectMapper mediaSubjectMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MediaSubject queryById(Integer id) {
        MediaSubject mediaSubject = mediaSubjectMapper.selectById(id);
        return mediaSubject;
    }


    /**
     * 查询
     *
     * @return 查询结果
     */
    @Override
    public ResultVO queryList() {
        String websiteId = WebsiteUtil.getWebsiteId();
        QueryWrapper<MediaSubject> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(websiteId)) {
            queryWrapper.eq("website_id", websiteId);
        }
        List<MediaSubject> mediaSubjects = mediaSubjectMapper.selectList(queryWrapper);
        return ResultVO.data(mediaSubjects);
    }


    /**
     * 新增数据
     *
     * @param mediaSubject 实例对象
     * @return 实例对象
     */
    @Override
    public ResultVO insert(MediaSubject mediaSubject) {
        String websiteId = WebsiteUtil.getWebsiteId();
        if (StringUtils.isNotBlank(websiteId)) {
            mediaSubject.setWebsiteId(websiteId);
        }
        if (mediaSubject == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (checkDuplicate(mediaSubject)) return ResultVO.fail(ResultCode.SUBJECT_DUPLICATION);
        mediaSubjectMapper.insert(mediaSubject);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    /**
     * 修改数据
     *
     * @param mediaSubject 实例对象
     * @return 实例对象
     */
    @Override
    public ResultVO update(MediaSubject mediaSubject) {
        if (mediaSubject == null && mediaSubject.getId() == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (queryById(mediaSubject.getId()) == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (checkDuplicate(mediaSubject)) return ResultVO.fail(ResultCode.SUBJECT_DUPLICATION);
        mediaSubjectMapper.updateById(mediaSubject);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    private boolean checkDuplicate(MediaSubject mediaSubject) {
        QueryWrapper<MediaSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("subject_name", mediaSubject.getSubjectName());
        String websiteId = WebsiteUtil.getWebsiteId();
        if (websiteId != null) {
            queryWrapper.eq("website_id", websiteId);
        }
        List<MediaSubject> mediaSubjects = mediaSubjectMapper.selectList(queryWrapper);
        if (mediaSubjects.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public ResultVO deleteById(Integer id) {
        if (id == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        mediaSubjectMapper.deleteById(id);
        return ResultVO.success(ResultCode.SUCCESS);
    }


}
